package com.ltcode.sys.interfaces.controller;

import com.ltcode.common.base.web.domain.ApiResponse;
import com.ltcode.sys.application.service.SysDeptAppService;
import com.ltcode.sys.domain.model.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 部门信息控制器
 * 
 * @author ltcode
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptAppService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public ApiResponse<List<SysDept>> list(SysDept dept) {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return ApiResponse.ok(depts);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{deptId}")
    public ApiResponse<List<SysDept>> excludeChild( @PathVariable Long deptId) {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        depts.removeIf(d -> d.getId().intValue() == deptId || d.getAncestors().contains("," + deptId + ","));
        return ApiResponse.ok(depts);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/{deptId}")
    public ApiResponse<SysDept> getInfo( @PathVariable Long deptId) {
        deptService.checkDeptDataScope(deptId);
        return ApiResponse.ok(deptService.selectDeptById(deptId));
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public ApiResponse<List<SysDept>> treeselect(SysDept dept) {
        List<SysDept> depts = deptService.selectDeptTreeList(dept);
        return ApiResponse.ok(depts);
    }

    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    public ApiResponse<?> roleDeptTreeselect( @PathVariable("roleId") Long roleId) {
        List<SysDept> depts = deptService.selectDeptTreeList(new SysDept());
        List<Long> checkedKeys = deptService.selectDeptListByRoleId(roleId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("depts", depts);
        map.put("checkedKeys", checkedKeys);
        return ApiResponse.ok(map);
    }

    /**
     * 新增部门
     */
    @PostMapping
    public ApiResponse<Void> add(@Validated @RequestBody SysDept dept) {
        if (!deptService.checkDeptNameUnique(dept)) {
            return ApiResponse.fail("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        deptService.insertDept(dept);
        return ApiResponse.ok();
    }

    /**
     * 修改部门
     */
    @PutMapping
    public ApiResponse<Void> edit(@Validated @RequestBody SysDept dept) {
        Long deptId = dept.getId();
        deptService.checkDeptDataScope(deptId);
        if (!deptService.checkDeptNameUnique(dept)) {
            return ApiResponse.fail("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        } else if (dept.getParentId().equals(deptId)) {
            return ApiResponse.fail("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        } else if ("1".equals(dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
            return ApiResponse.fail("该部门包含未停用的子部门！");
        }
        deptService.updateDept(dept);
        return ApiResponse.ok();
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{deptId}")
    public ApiResponse<Void> remove( @PathVariable Long deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return ApiResponse.fail("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return ApiResponse.fail("部门存在用户,不允许删除");
        }
        deptService.checkDeptDataScope(deptId);
        deptService.deleteDeptById(deptId);
        return ApiResponse.ok();
    }
}
